//
//  FFRecvPayCardOffCell.m
//  LLWallet
//
//  Created by tt on 2020/12/17.
//

#import "FFRecvPayCardOffCell.h"

@implementation FFRecvPayCardOffModel

- (Class)cell {
    return FFRecvPayCardOffCell.class;
}

@end

@interface FFRecvPayCardOffCell ()


@property(nonatomic,strong) FFRecvPayCardOffModel *model;

@property(nonatomic,strong) UIView *boxView;

@property(nonatomic,strong) UIButton *moreBtn;

@property(nonatomic,strong) UIImageView *recvPayIcon;
@property(nonatomic,strong) UILabel *titleLbl;

@property(nonatomic,strong) UIView *bodyView;

@property(nonatomic,strong) UIImageView *payWarnIcon;
@property(nonatomic,strong) UILabel *payTipLbl;

@property(nonatomic,strong) UIButton *openBtn;

@property(nonatomic,strong) UIActivityIndicatorView *indicatorView;


@end

#define headerHeight 64.0f
#define barcodeHeight 140.0f

@implementation FFRecvPayCardOffCell


+ (CGSize)sizeForModel:(WKFormItemModel *)model {
    return CGSizeMake(WKScreenWidth, 500.0f);
}

- (void)setupUI {
    [super setupUI];
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.boxView];
    
    [self.boxView addSubview:self.moreBtn];
    
    [self.boxView addSubview:self.recvPayIcon];
    [self.boxView addSubview:self.titleLbl];
    
    [self.boxView addSubview:self.bodyView];
    
    [self.bodyView addSubview:self.payWarnIcon];
    [self.bodyView addSubview:self.payTipLbl];
    [self.bodyView addSubview:self.openBtn];
    
    [self.bodyView addSubview:self.indicatorView];

    
    
}

- (void)refresh:(FFRecvPayCardOffModel *)model {
    [super refresh:model];
    
    self.model = model;
    
    self.payWarnIcon.hidden = YES;
    self.payTipLbl.hidden = YES;
    self.openBtn.hidden = YES;
    self.indicatorView.hidden = YES;
    if(!model.loading) {
        self.indicatorView.hidden = YES;
        [self.indicatorView stopAnimating];
        self.payWarnIcon.hidden = NO;
        self.payTipLbl.hidden = NO;
        self.openBtn.hidden = NO;
    }else{
        self.indicatorView.hidden = NO;
        [self.indicatorView startAnimating];
    }

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.boxView.lim_left = 10.0f;
    self.boxView.lim_height = self.contentView.lim_height;
    self.boxView.lim_width = self.contentView.lim_width - self.boxView.lim_left*2;
    self.boxView.lim_centerX_parent = self.contentView;
    
    self.moreBtn.lim_left = self.boxView.lim_width - self.moreBtn.lim_width - 15.0f;
    self.moreBtn.lim_top = headerHeight/2.0f - self.moreBtn.lim_height/2.0f;
    
    self.recvPayIcon.lim_left = 15.0f;
    self.recvPayIcon.lim_top = headerHeight/2.0f - self.recvPayIcon.lim_height/2.0f;
    
    self.titleLbl.lim_top =  headerHeight/2.0f - self.titleLbl.lim_height/2.0f;
    self.titleLbl.lim_left = self.recvPayIcon.lim_right + 10.0f;
    
    self.bodyView.lim_top = headerHeight;
    self.bodyView.lim_height = self.boxView.lim_height - headerHeight;
    self.bodyView.lim_width = self.boxView.lim_width;
    
    self.payWarnIcon.lim_top = 40.0f;
    self.payWarnIcon.lim_centerX_parent = self.bodyView;
    
    self.payTipLbl.lim_width = self.bodyView.lim_width - 20*2;
    [self.payTipLbl sizeToFit];
    
    self.payTipLbl.lim_top = self.payWarnIcon.lim_bottom + 20.0f;
    self.payTipLbl.lim_centerX_parent = self.bodyView;
    
    self.openBtn.lim_top = self.payTipLbl.lim_bottom + 80.0f;
    self.openBtn.lim_centerX_parent = self.bodyView;
    
    if(self.model.loading) {
        self.indicatorView.lim_size = self.bodyView.lim_size;
    }
    
}

- (UIView *)boxView {
    if(!_boxView) {
        _boxView = [[UIView alloc] init];
        _boxView.backgroundColor = [UIColor colorWithRed:249.0f/255.0f green:249.0f/255.0f blue:249.0f/255.0f alpha:1.0f];
        _boxView.layer.masksToBounds = YES;
        _boxView.layer.cornerRadius = 8.0f;
    }
    return _boxView;
}

- (UIImageView *)recvPayIcon {
    if(!_recvPayIcon) {
        _recvPayIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 24.0f, 24.0f)];
        _recvPayIcon.image = [self imageName:@"recvpay_icon"];
    }
    return _recvPayIcon;
}

- (UILabel *)titleLbl {
    if(!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.textColor = [WKApp shared].config.themeColor;
        _titleLbl.font = [[WKApp shared].config appFontOfSize:17.0f];
        _titleLbl.text = LLang(@"向商家付款");
        [_titleLbl sizeToFit];
    }
    return _titleLbl;
}

- (UILabel *)payTipLbl {
    if(!_payTipLbl) {
        _payTipLbl = [[UILabel alloc] init];
        _payTipLbl.text = LLang(@"需要重新开启刷卡功能，开启后向商家出示付款即可完成快速支付。");
        _payTipLbl.font = [[WKApp shared].config appFontOfSize:16.0f];
        _payTipLbl.textColor = [UIColor colorWithRed:49.0f/255.0f green:49.0f/255.0f blue:49.0f/255.0f alpha:1.0f];
        _payTipLbl.numberOfLines = 0;
        _payTipLbl.lineBreakMode = NSLineBreakByWordWrapping;
    }
    return _payTipLbl;
}

- (UIView *)bodyView {
    if(!_bodyView) {
        _bodyView = [[UIView alloc] init];
        _bodyView.backgroundColor = [UIColor whiteColor];
    }
    return _bodyView;
}

- (UIImageView *)payWarnIcon {
    if(!_payWarnIcon) {
        _payWarnIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 48.0f, 48.0f)];
        _payWarnIcon.image = [self imageName:@"pay_warn"];
    }
    return _payWarnIcon;
}

- (UIButton *)openBtn {
    if(!_openBtn) {
        _openBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 240.0f, 40.0f)];
        [_openBtn setTitle:LLang(@"开启") forState:UIControlStateNormal];
        _openBtn.backgroundColor = [WKApp shared].config.themeColor;
        _openBtn.layer.masksToBounds = YES;
        _openBtn.layer.cornerRadius = 4.0f;
        [_openBtn addTarget:self action:@selector(openPressed) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _openBtn;
}

-(void) openPressed {
    if(self.model && self.model.onOpen) {
        self.model.onOpen();
    }
}

-(UIActivityIndicatorView*) indicatorView {
    if(!_indicatorView) {
        _indicatorView = [[UIActivityIndicatorView alloc] init];
    }
    return _indicatorView;
}


-(UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LLWallet"];
}


@end
